package com.bii.gelapp;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class DNAValueActivity extends Activity {
	public Menu menu;
	public Bitmap originalBmp; // origin
	public static ArrayList<ConnectedComponent> markerlist = new ArrayList<ConnectedComponent>();
	public static ArrayList<ConnectedComponent> componentlist = new ArrayList<ConnectedComponent>();
	public static ArrayList<Integer> markervalues = new ArrayList<Integer>();
	public DNAValueTouchImageView imgView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dnavalueactivity);
		
		// instructions
		String donotshow = "";
		final Dialog instructiondialog = new Dialog(DNAValueActivity.this);
		instructiondialog.setContentView(R.layout.instructiondialog);
		instructiondialog.setTitle("Attention");
		instructiondialog.setCancelable(true);
		instructiondialog.setCancelable(false);

		TextView tvInstructions = (TextView) instructiondialog
				.findViewById(R.id.tvInstructions);
		ImageView dialogIvImage = (ImageView) instructiondialog
				.findViewById(R.id.ivInstructions);
		final CheckBox donotshowbox = (CheckBox) instructiondialog.findViewById(R.id.donotshow);
		Button btOk = (Button) instructiondialog.findViewById(R.id.btOk);

		final SharedPreferences donotshowinstructions = this.getSharedPreferences(
				"instructions", 0);
		donotshow = donotshowinstructions.getString("dnavalueinstructions",
				"nothing");
		if (donotshow.equals("nothing")) {
			donotshow = "Show";
		}

		if (donotshow.equals("Show")) {
			tvInstructions.setText("Enter the size of markers manually by clicking on the yellow rectangles");
			
			Drawable drawable = getResources().getDrawable(R.drawable.valueediting_instruction);

			Bitmap instructionbmp = ((BitmapDrawable)drawable).getBitmap();
			
			dialogIvImage.setImageBitmap(instructionbmp);
			
			instructiondialog.show();

			btOk.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if(donotshowbox.isChecked()){
						SharedPreferences.Editor editor = donotshowinstructions.edit();
						editor.putString("dnavalueinstructions", "DoNotShow");
						editor.commit();
					}
					else
					{
						SharedPreferences.Editor editor = donotshowinstructions.edit();
						editor.putString("dnavalueinstructions", "Show");
						editor.commit();
					}
					instructiondialog.dismiss();
				}
			});
			
		}
		//
		
		if (getIntent().hasExtra("markerlist")) {
			markerlist.clear();
			markerlist = (ArrayList<ConnectedComponent>) getIntent()
					.getSerializableExtra("markerlist");
		}
		if (getIntent().hasExtra("componentlist")) {
			componentlist.clear();
			componentlist = (ArrayList<ConnectedComponent>) getIntent()
					.getSerializableExtra("componentlist");
		}
		markervalues = null;
		markervalues = new ArrayList();
		originalBmp = RotationActivity.colorBmp;
		for (int i = 0; i < markerlist.size(); i++) {
			markervalues.add(0);
		}

		imgView = new DNAValueTouchImageView(this, originalBmp);

		imgView.setMaxZoom(4f);

		setContentView(imgView);
		Toast.makeText(DNAValueActivity.this, "Click on markers to edit the values", Toast.LENGTH_LONG);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu1) {
		// TODO Auto-generated method stub
		this.menu = menu1;
		getMenuInflater().inflate(R.menu.valuemenu, menu1);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		super.onOptionsItemSelected(item);
		switch (item.getItemId()) {
		case R.id.btConfirm:
			// Intent returnActivity = new
			// Intent(CompareFullScreenActivity.this,
			// CompareActivity.class);
			// CompareActivity.markervalues =
			// CompareFullScreenActivity.markervalues;
			// startActivity(returnActivity);
			Intent goToNextActivity = new Intent(DNAValueActivity.this,
					DNACalculateActivity.class);
			
			goToNextActivity.putExtra("componentlist", componentlist);

			goToNextActivity.putExtra("markerlist", markerlist);
			goToNextActivity.putExtra("markervalues", markervalues);
			startActivity(goToNextActivity);

			break;

		case R.id.btCancel:
			markervalues = null;
			markervalues = new ArrayList();
			originalBmp = RotationActivity.colorBmp;
			for (int i = 0; i < markerlist.size(); i++) {
				markervalues.add(0);
			}

			imgView = new DNAValueTouchImageView(this, originalBmp);

			imgView.setMaxZoom(4f);

			setContentView(imgView);
			break;

		}
		return true;
	}
}
